{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f908d604-0ee4-48b6-be2c-c49fc9854ee0",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 自定义函数中混合使用*args和**kwargs\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eaac1b1d-f302-4861-b260-2b6f36957d4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import statistics\n",
    "# 混合 *args, **kwargs\n",
    "def calc_stats(operation, *args, **kwargs):\n",
    "    result = 0\n",
    "    # 计算标准差\n",
    "    if operation == \"stdev\":\n",
    "        # 总体标准差\n",
    "        if \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'population':\n",
    "            result = statistics.pstdev(args)\n",
    "        # 样本标准差\n",
    "        elif \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'sample':\n",
    "            result = statistics.stdev(args) \n",
    "        else:\n",
    "            raise ValueError('TYPE, either population or sample')\n",
    "    # 计算方差\n",
    "    elif operation == \"var\":\n",
    "        # 总体方差\n",
    "        if \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'population':\n",
    "            result = statistics.pvariance(args)\n",
    "        # 样本方差\n",
    "        elif \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'sample':\n",
    "            result = statistics.variance(args)  \n",
    "        else:\n",
    "            raise ValueError('TYPE, either population or sample')\n",
    "    else:\n",
    "        print(\"Unsupported operation\")\n",
    "        return None\n",
    "    # 保留小数位\n",
    "    if \"ROUND\" in kwargs:\n",
    "        result = round(result, kwargs[\"ROUND\"])\n",
    "    return result\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "01a60e62-2110-4731-a3c3-1184ddbbc63b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.708"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算总体标准差\n",
    "calc_stats(\"stdev\", 1, 2, 3, 4, 5, 6, \n",
    "            TYPE = 'population', ROUND = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "80c31372-1b70-43ae-9d05-bbfee8b4c49d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.8708286933869707"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算样本标准差\n",
    "calc_stats(\"stdev\", 1, 2, 3, 4, 5, 6, TYPE = 'sample')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e6c86688-1226-4c99-9d98-75170cae6d8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9167"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算总体方差\n",
    "calc_stats(\"var\", 1, 2, 3, 4, 5, 6, \n",
    "           TYPE = 'population', ROUND = 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "844d0cab-0bbc-4379-8faa-a070f9f1f377",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算样本方差\n",
    "calc_stats(\"var\", 1, 2, 3, 4, 5, 6, TYPE = 'sample')    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
